package u.hnx.admin.utils;

import jakarta.servlet.http.HttpServletRequest;
import u.hnx.admin.query.user.LoginQuery;

public class RequestContextHelper {
    private static final ThreadLocal<String> requestIdHolder = new ThreadLocal<>();
    private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<>();
    private static final ThreadLocal<LoginQuery> contextHolder = new ThreadLocal<>();
    public static void setRequestId(String requestId) {
        requestIdHolder.set(requestId);
    }

    public static String getRequestId() {
        return requestIdHolder.get();
    }
    public static HttpServletRequest getRequest(){
        return requestHolder.get();
    }
    public static void setRequest(HttpServletRequest request){
        requestHolder.set(request);
    }
    public static void setContext(LoginQuery context) {
        contextHolder.set(context);
    }

    public static LoginQuery getContext() {
        return contextHolder.get();
    }
    public static void clear() {
        requestIdHolder.remove();
        LoginQuery context = contextHolder.get();
        if (context != null) {
            context = null;
        }
        contextHolder.remove();
        HttpServletRequest httpServletRequest = requestHolder.get();
        if(httpServletRequest != null){
            httpServletRequest = null;
        }
        requestHolder.remove();
    }

    public static String generateRequestId() {
        return cn.hutool.core.lang.UUID.fastUUID().toString(true);
    }
}
